package edu.bit.water.controller;

import edu.bit.water.dao.EvaWaterSensorInfoDao;
import edu.bit.water.entity.EvaWaterSensorInfo;
import edu.bit.water.service.EvaWaterSensorInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/eva_watersensor_info")
public class EvaWaterSensorInfoController extends BaseController{
	@Autowired
    private EvaWaterSensorInfoDao evaWaterSensorInfoDao;
	
    @Autowired
    private EvaWaterSensorInfoService evaWaterSensorInfoService;

    /**
     * @Description: 查询watersensor
     * @return: Map
     * @Author: YYF
     */
    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> queryEvaWaterSensorInfo(int company_id) {
        //取出在当前用户的id
        int currentUserId=currentUserId();

        //从数据库中获取数据
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("rows", evaWaterSensorInfoService.queryEvaWaterSensorInfo(company_id,currentUserId));
        return modelMap;
    }

    /**
     * @Description: 批量删除watersensor, 默认删除成功
     * @Param: 删除的设备号list
     * @return: Map
     * @Author: YYF
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map<String, String> delete(@RequestBody List<Integer> ids) {
        StringBuilder temp = new StringBuilder(ids.toString());
        temp.setCharAt(0, '(');
        temp.setCharAt(temp.length() - 1, ')');
        evaWaterSensorInfoService.deleteEvaWaterSensorInfo(new String(temp));

        Map<String, String> modelMap = new HashMap<>();
        modelMap.put("status", "success");
        return modelMap;
    }

    /**
     * @Description: 插入
     * @Param:  EvaWaterSensorInfo
     * @return: Map
     * @Author: YYF
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Map<String,Object> insertEvaWaterSensorInfo(EvaWaterSensorInfo evaWaterSensorInfo){
        //return evaWaterSensorInfoService.insertEvaWaterSensorInfo(evaWaterSensorInfo);
    	Map<String,Object> modelMap = new HashMap<>();
    	//判断当前用户是否存在
    	if(currentUser()==null) {
			modelMap.put("status", "failed");
			return modelMap;
		}
        try {
    	    //获取当前用户id
        	int userid=currentUser().getId();
        	evaWaterSensorInfo.setUserId(userid);
            evaWaterSensorInfoDao.insertEvaWaterSensorInfo(evaWaterSensorInfo);
            modelMap.put("status","success");
        }catch (DuplicateKeyException e){
            modelMap.put("status","failed");
            modelMap.put("msg","水表编号已存在");
        }
        return modelMap;
    }

    /**
     * @Description: 更新
     * @Param:  EvaWaterSensorInfo
     * @return: Map
     * @Author: YYF
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> updateEvaWaterSensorInfo(EvaWaterSensorInfo evaWaterSensorInfo){
        //return evaWaterSensorInfoService.updateEvaWaterSensorInfo(evaWaterSensorInfo);
    	 Map<String,Object> modelMap = new HashMap<>();
    	 if(currentUser()==null) {
 			modelMap.put("status", "failed");
 			return modelMap;
 		}
         try {
        	 int userid=currentUser().getId();
         	 evaWaterSensorInfo.setUserId(userid);
             evaWaterSensorInfoDao.updateEvaWaterSensorInfo(evaWaterSensorInfo);
             modelMap.put("status","success");
         }catch (DuplicateKeyException e){
             modelMap.put("status","failed");
             modelMap.put("msg","水表编号已存在");
         }
         return modelMap;
    }

    /**
     * @Description: 查询entitysensor中所有Id和name
     * @Author: YYF
     */
    @ResponseBody
    @RequestMapping("/query_entitysensor_id_name")
    public List<Map<String, Object>> queryEntitySensorIdAndName() {
        return evaWaterSensorInfoService.selectEntitySensorIdAndName();
    }

    /**
     * @Description: 根据entitysensorId查询entitysensor中字段及值
     * @Param: int
     * @return: Map
     * @Author: YYF
     */
    @ResponseBody
    @RequestMapping("/query_entitysensor_by_id")
    public Map<String, Object> queryEntitySensorById(int id) {
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> temp = evaWaterSensorInfoService.selectEntitySensorById(id);
        if (temp.size() != 0) {
            modelMap.put("status", "success");
            modelMap.put("rows", temp);
        } else {
            modelMap.put("status", "failed");
        }
        return modelMap;
    }

    @RequestMapping("/query_entity_sensor_cid")
    @ResponseBody
    public Map<String, Object> query_entity_sensor_cid(int testId) {
       //从数据库中获取数据
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("rows", evaWaterSensorInfoService.query_entity_sensor_cid(testId));
        return modelMap;
    }
}
